@using Duende.Bff.Blazor.Client
@using Microsoft.Extensions.Options

@rendermode InteractiveAuto

@inject IRenderModeContext RenderModeContext
@inject IOptions<BffBlazorClientOptions> Options

<AuthorizeView>
    <Authorized>
        <strong>Hello, @context.User.Identity?.Name</strong>
        <a class="nav-link btn btn-link" href="@BffLogoutUrl(context)">Log out</a>
    </Authorized>
    <Authorizing>
        <a class="nav-link btn btn-link disabled">Log in</a>
    </Authorizing>
    <NotAuthorized>
        <a class="nav-link btn btn-link" href="bff/login">Log in</a>
    </NotAuthorized>
</AuthorizeView>


@code {
    string BffLogoutUrl(AuthenticationState context)
    {
        var logoutUrl = context.User.FindFirst(Constants.ClaimTypes.LogoutUrl);
        if (logoutUrl == null) throw new InvalidOperationException("The logout url is missing");
        return $"{Options.Value.StateProviderBaseAddress}{logoutUrl.Value}";
    }
}
